package com.george.rocketmq.entity.dto;

import com.george.rocketmq.constants.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.regex.Pattern;

/**
 * @title: Result.java
 * @description: 统一响应实体
 * @author: George
 * @date: 2023/3/24 12:07
 */
@ApiModel
public class Result<T> implements Serializable {

    private static final Pattern PATTERN_CHINESE = Pattern.compile("[\u4e00-\u9fa5].*[\\u4e00-\\u9fa5]");

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("响应数据")
    private T data;

    @ApiModelProperty("响应码")
    private int code;

    @ApiModelProperty("响应提示")
    private String message;

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Result successResult() {
        return successResult(null);
    }

    public static Result successResult(String message) {
        return new Result(ResultCodeEnum.SUCCESS_200.getCode(), message);
    }

    public static <T> Result successResult(ResultCodeEnum resultCodeEnum, T data) {
        return new Result(resultCodeEnum.getCode(), resultCodeEnum.getDesc(), data);
    }

    public static <T> Result successResult(String message, T data) {
        return new Result(ResultCodeEnum.SUCCESS_200.getCode(), message, data);
    }

    public static <T> Result successResult(int code, String message, T data) {
        return new Result(code, message, data);
    }

    public static <T> Result successResult(T data) {
        Result result = new Result(data);
        result.setCode(ResultCodeEnum.SUCCESS_200.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS_200.getDesc());
        return result;
    }

    public static Result failResult() {
        return failResult(null);
    }

    public static Result failResult(String message) {
        return new Result(ResultCodeEnum.FAILURE_500.getCode(), message);
    }


    public static Result failResult(int code, String message) {
        return new Result(code, message);
    }

    public static <T> Result failResult(ResultCodeEnum resultCodeEnum, T data) {
        return new Result(resultCodeEnum.getCode(), resultCodeEnum.getDesc(), data);
    }

    public static <T> Result failResult(int code, String message, T data) {
        return new Result(code, message, data);
    }

    public static <T> Result failResult(T data) {
        Result result = new Result(data);
        result.setCode(ResultCodeEnum.FAILURE_500.getCode());
        result.setMessage(ResultCodeEnum.FAILURE_500.getDesc());
        return result;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
